//
//  HQMapTools.m
//  o_amap
//
//  Created by Q神 on 2021/4/7.
//

#import "HQMapTools.h"

@implementation HQMapTools

+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    static HQMapTools * manager = nil;
    dispatch_once(&onceToken, ^{
        manager = [[HQMapTools alloc] init];
    });
    return manager;
}

/**
 将字典转换成json格式字符串,不含\n这些符号
 */
- (NSString *)hq_jsonStringCompactFormatForDictionary:(NSDictionary *)dicJson {

    if (![dicJson isKindOfClass:[NSDictionary class]] || ![NSJSONSerialization isValidJSONObject:dicJson]) {
        return nil;
    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicJson options:0 error:nil];
    NSString *strJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return strJson;
}

/**
 将字典转换成json格式字符串,含\n这些符号,便于阅读
 */
- (NSString *)hq_jsonStringPrettyPrintedFormatForDictionary:(NSDictionary *)dicJson {
    
    if (![dicJson isKindOfClass:[NSDictionary class]] || ![NSJSONSerialization isValidJSONObject:dicJson]) {
        return nil;
    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicJson options:NSJSONWritingPrettyPrinted error:nil];
    NSString *strJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return strJson;
}
 
/**
 将数组转换成json格式字符串,不含\n这些符号
 */
- (NSString *)hq_jsonStringCompactFormatForNSArray:(NSArray *)arrJson {
    
    if (![arrJson isKindOfClass:[NSArray class]] || ![NSJSONSerialization isValidJSONObject:arrJson]) {
        return nil;
    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arrJson options:0 error:nil];
    NSString *strJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return strJson;
}

/**
 json字符串转字典
 */
- (NSDictionary*)parseJSONStringToNSDictionary:(NSString*)JSONString {
    
    NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
    return responseJSON;
}

/**
 json字符串转字典
 */
- (NSArray *)parseJSONStringToNSArray:(NSString *)JSONString {
    
    NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
    return responseJSON;
}

/**
 格式化时间
 */
- (NSString *)getFormatTime:(NSDate*)date {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *timeString = [formatter stringFromDate:date];
    return timeString;
}

/**
 判断字符串为空
 */
- (BOOL)isBlankString:(NSString *)aStr {
    if (!aStr) {
        return NO;
    }
    if ([aStr isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if (!aStr.length) {
        return NO;
    }
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimmedStr = [aStr stringByTrimmingCharactersInSet:set];
    if (!trimmedStr.length) {
        return NO;
    }
    return YES;
}


- (NSString *)getAppPluginImageWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar withImageName:(NSString *)name {
    
    /// Flutter 传过来的图片路径
    NSString *iconName = name;
    NSLog(@"Flutter 传过来的图片路径 == %@", iconName);
    
    /// 获取插件控制器
    FFileManager *fileManager = [FFileManager defaultCenter];
    fileManager.registar = registrar;
//    NSLog(@"Flutter 插件控制器 == %@", registrar);
    
    /// iOS 插件获取 flutter 里面图片资源路径
    NSString *path = [fileManager pathForFlutterImageName: iconName];
    
    NSLog(@"iOS 插件获取 flutter 里面图片资源路径 == %@", path);
    
    return path;
}

- (UIImage *)getAppPluginImageWithPath:(NSString *)path {
    /// 通过 UIImage 来加载一个File
    UIImage *icon = [UIImage imageWithContentsOfFile:path];
    return icon;
}

- (NSData *)getBundlePathWithName:(NSString *)name {
    NSString *path = [NSString stringWithFormat:@"%@/%@", [NSBundle mainBundle].bundlePath, name];
    NSData *data = [NSData dataWithContentsOfFile:path];
    return data;
}

- (UIImage *)getAppPluginImageWithData:(NSData *)data {
    /// 通过 UIImage 来加载一个data
    UIImage *icon = [UIImage imageWithData:data];
    return icon;
}

/**
 *  改变图片的大小
 *
 *  @param img     需要改变的图片
 *  @param newsize 新图片的大小
 *
 *  @return 返回修改后的新图片
 */
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize {
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(newsize);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}

@end
